#!/usr/bin/env python -OO
# -*- coding: utf-8 -*-

# Copyright © 2008,2009 Trust Digital Technology Laboratory, Shanghai Jiao Tong University.
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice (including the next
# paragraph) shall be included in all copies or substantial portions of the
# Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# Author:
#   XING Dong sheng <homer.xing@gmail.com>

from __future__ import with_statement

__all__ = ['installcomicplayer']

import os, sys, stat
from softwaredependent import *
from softwareindependent import getArch, TempOwn

def xpi():
	execute("cd %s; firefox comicview-0.2.7.xpi &"%LOCAL)

def codec():
	if os.system("sudo mkdir /usr/lib/codecs"):
		if execute("cd %s; tar jxf essential-20071007.tar.bz2 && sudo mv essential-20071007/* /usr/lib/codecs/"%LOCAL):
			if getArch()==64:
				if execute("cd %s; tar jxf essential-amd64-20071007.tar.bz2 ; sudo mv essential-amd64-20071007/* /usr/lib/codecs/"%LOCAL):
					execute("cd %s; sudo mkdir /usr/lib/win32/; unzip windows-essential-20071007.zip ; sudo mv windows-essential-20071007/* /usr/lib/win32/"%LOCAL)

def blue():
	os.system("cd %s; sudo mkdir /usr/local/share/mplayer/skins/ -p; tar jxf Blue-1.7.tar.bz2 ; sudo mv Blue /usr/local/share/mplayer/skins/default"%LOCAL)

def compilemplayer():
	install("libgtk2.0-dev libpng12-dev xorg-dev")
	if install("mplayer"):
			if execute("cd %s &&"
" sudo rm mplayer_1.0~rc2.orig -rf &&"
" tar zxf mplayer-1.0rc2.orig.tar.gz &&"
" cd mplayer_1.0~rc2.orig &&"
" zcat ../1.0-rc2.patch.gz | patch -p1 >/dev/null &&"
" zcat ../1.0-rc2.ivtv.patch.gz | patch -p1 >/dev/null "%LOCAL):
				execute("cd %s/mplayer_1.0~rc2.orig &&"
" ./configure --enable-gui --enable-freetype --libdir=/usr/lib --codecsdir=/usr/lib/codecs --win32codecsdir=/usr/lib/win32 >/dev/null &&"
" make >/dev/null 2>/dev/null && sudo make install"%LOCAL)
	execute("sudo apt-get remove libgtk2.0-dev libpng12-dev xorg-dev -qq")
	execute("sudo apt-get autoremove -qq")

def ttf():
	execute("mkdir ~/.mplayer/; cp %s/simhei.ttf ~/.mplayer/subfont.ttf"%LOCAL )

def config():
	execute("mkdir ~/.mplayer/; cp %s ~/.mplayer/config"%(FILES+"mplayerconfig") )

def compileconverter():
	execute("sudo gcc -O3 %s -o /usr/local/bin/convert_"%(FILES+"convert_.c") )
	execute("sudo chmod a+x /usr/local/bin/convert_")

def converterscript():
	execute("sudo cp %s /usr/local/bin/"%(FILES+"playcomic") )
	execute("sudo chmod a+x /usr/local/bin/playcomic")

def installcomicplayer():
	f=download("comicplayer")
	execute("cd %s; tar zxf %s"%(LOCAL, f) )
	xpi()
	codec()
	compilemplayer()
	blue()
	ttf()
	config()
	compileconverter()
	converterscript()

if __name__ == "__main__":
	installcomicplayer()
